<?php

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class AttendancesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    	$attendances = [];
    	$user = User::with('schedules.project')->where('id', 1)->firstOrFail();
    	foreach ($user->schedules as $schedule) {
    		$attendances[] = [
    			'user_id' => $user->id,
    			'company_id' => $user->company_id,
    			'project_id' => $user->project_id,
    			'schedule_id' => $schedule->id,
    			'checkin_longitude' => 128.2828332,
    			'checkin_latitude' => 28.2828332,
    			'checkin_address' => '',
    			'checkin_example' => 100002,
    			'checkin_image' => '/var/images/login.jpg',
    			'checkin_time' => $this->random($schedule->start_timestamp),
    			'checkin_status' => collect(['tardy', 'early', 'normal'])->random(),
    			'checkout_longitude' => 128.2828332,
    			'checkout_latitude' => 28.2828332,
    			'checkout_address' => '',
    			'checkout_example' => 100008,
    			'checkout_image' => '/var/images/test.jpg',
    			'checkout_time' => $this->random($schedule->end_timestamp),
    			'checkout_status' => collect(['tardy', 'early', 'normal'])->random(),
    			'genre_id' => $schedule->genre_id,
    			'genre_name' => $schedule->genre_name
			];
		}

		DB::table('attendances')->insert($attendances);
    }

	/**
	 * 生成随机时间
	 *
	 * Date: 30/04/2018
	 * @author George
	 * @param string $datetime
	 * @return false|string
	 */
	protected function random(string $datetime)
	{
		$time = strtotime($datetime);
		$random = rand(-8, 8) * 200;
		return date('Y-m-d H:i:s', $time + $random);
    }
}
